package com.richapp.launcher.scene.conditions.sensor;

import com.richapp.launcher.database.system.dao.ConditionSensorModelDao;
import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractSensorConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneSensorConditions;

public class SceneSensorTemperatureHumidityConditions extends SceneAbstractSensorConditions {
    ConditionSensorModelDao conditionSensorModelDao=null;

    private int checkTemperatureValue;
    private int checkHumidityValue;

    public SceneSensorTemperatureHumidityConditions() {

    }

    @Override
    public int getConditionType() {
        return ISceneConditions.SCENE_CONDITION_TYPE_SENSOR;
    }

    @Override
    public int getConditionSubType() {
        return ISceneSensorConditions.SCENE_CONDITION_SUB_TYPE_SENSOR_CO2;
    }

    @Override
    public boolean checkCondition(String sceneName) {

        boolean flag=false;
        DeviceModel device = getDevice();
        ConditionSensorModel conditionSensorModel;
        SceneModel sceneModel ;
        SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
        conditionSensorModelDao=new ConditionSensorModelDao(SceneServer.getInstance().getContext());
        if (sceneName.equals("")){
            return false;
        }
        sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
         conditionSensorModel= conditionSensorModelDao.getConditionSensorModel(sceneName);
        int checkModel= Integer.parseInt(conditionSensorModel.getConditionCheckModel());
        if (device != null) {
            double tValue = device.getTemperature();
            int hValue = device.getHumidity();

            if ( conditionSensorModel.getCheckTemperatureValue()!=-1314) {
            if (checkModel > -1) {
                switch (checkModel) {
                    case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_GREATERTHAN:
                        if (tValue > checkTemperatureValue && "true".equals(sceneModel.getStatusCycleFlag())) {
                            flag = true;
                        }else if (tValue <= checkTemperatureValue){
                            sceneModel.setStatusCycleFlag("true");
                            sceneModelDao.add(sceneModel);
                        }
                        break;

                    case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_EQUALS:
                        if (tValue == checkTemperatureValue&& "true".equals(sceneModel.getStatusCycleFlag())) {
                            flag = true;
                        }else if (tValue != checkTemperatureValue){
                            sceneModel.setStatusCycleFlag("true");
                            sceneModelDao.add(sceneModel);
                        }
                        break;

                    case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_LESSTHAN:
                        if (tValue < checkTemperatureValue && "true".equals(sceneModel.getStatusCycleFlag())) {
                            flag = true;
                        }else if (tValue >= checkTemperatureValue){
                            sceneModel.setStatusCycleFlag("true");
                            sceneModelDao.add(sceneModel);
                        }
                        break;

                    default:
                        break;
                }
            }
            }

            if (conditionSensorModel.getCheckHumidityValue()!=-1314){
            if (checkModel> -1) {
                switch (checkModel) {
                    case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_GREATERTHAN:
                        if (hValue > checkHumidityValue && "true".equals(sceneModel.getStatusCycleFlag())) {
                            flag = true;
                        }else if (hValue <= checkHumidityValue){
                            sceneModel.setStatusCycleFlag("true");
                            sceneModelDao.add(sceneModel);
                        }
                        break;

                    case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_EQUALS:
                        if (hValue == checkHumidityValue && "true".equals(sceneModel.getStatusCycleFlag())) {
                            flag = true;
                        }else if (hValue != checkHumidityValue){
                            sceneModel.setStatusCycleFlag("true");
                            sceneModelDao.add(sceneModel);
                        }
                        break;

                    case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_LESSTHAN:
                        if (hValue < checkHumidityValue && "true".equals(sceneModel.getStatusCycleFlag())) {
                            flag = true;
                        }else if (hValue >= checkHumidityValue){
                            sceneModel.setStatusCycleFlag("true");
                            sceneModelDao.add(sceneModel);
                        }
                        break;

                    default:
                        break;
                }
            }
            }
/*            if ((getConditionCheckModel()[0] > -1) && (getConditionCheckModel()[1] > -1)) {
                if (flag1 && flag2) {
                    return true;
                }
            } else if ((getConditionCheckModel()[0] > -1) && (getConditionCheckModel()[1] == -1)) {
                if (flag1) {
                    return true;
                }
            } else if ((getConditionCheckModel()[0] == -1) && (getConditionCheckModel()[1] > -1)) {
                if (flag2) {
                    return true;
                }
            }*/
        }

        return flag;
    }

    public int getCheckTemperatureValue() {
        return checkTemperatureValue;
    }

    public void setCheckTemperatureValue(int checkTemperatureValue) {
        this.checkTemperatureValue = checkTemperatureValue;
    }

    public int getCheckHumidityValue() {
        return checkHumidityValue;
    }

    public void setCheckHumidityValue(int checkHumidityValue) {
        this.checkHumidityValue = checkHumidityValue;
    }

}
